package com.atguigu.category.controller;

import com.atguigu.category.service.CategoryService;
import com.atguigu.param.PageParam;
import com.atguigu.param.ProductHotsParam;
import com.atguigu.pojo.Category;
import com.atguigu.utils.R;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("{categoryName}")
    public R detail(@PathVariable String categoryName){

        if(StringUtils.isNullOrEmpty(categoryName)){
            return R.fail("参数异常！");
        }
        return categoryService.detail(categoryName);
    }

    @PostMapping("names")
    public R names(@RequestBody @Validated ProductHotsParam productHotsParam, BindingResult result){
        if(result.hasErrors()){
            return R.fail("参数异常");
        }
        return categoryService.names(productHotsParam);
    }

    @GetMapping("list")
    public R list(){

        return categoryService.list();
    }


    @GetMapping("listCategory")
    public R listCategory(@RequestBody PageParam pageParam){
        return categoryService.listCategory(pageParam);
    }

    @PostMapping("save")
    public R save(@RequestBody Category category){
        return categoryService.save(category);
    }

    @PostMapping("remove")
    public R remove(@RequestBody Integer categoryId){
        return categoryService.remove(categoryId);
    }

    @PostMapping("update")
    public R update(@RequestBody Category category){
        return categoryService.update(category);
    }
}
